package com.example.send.util;

import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import com.google.common.collect.TreeRangeMap;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

public class Division2Util {

    public static void main(String[] args) throws InterruptedException {
        List<Double> listD = new ArrayList<>();
        for(int i = 1; i <= 9 ; i++) {
//            listD.add(1.0/i);
            listD.add(Math.round(i/0.01) * 0.01);
        }
        listD.add(1.0);
//        listD.add(1.5);
//        listD.add((int)(400/3)/100.0);
//        listD.add(5/4.0);
        listD.add(10.0);
        listD.sort(Comparator.naturalOrder());

        RangeMap<Double,Double> rangeMap = TreeRangeMap.create();
        double last = 0;
        double mid = 1.0;
        for(int i =0 ;i< listD.size();i++) {
            double key = listD.get(i);
            if(last == 0){
                last = key;
                continue;
            }
            double newMid = (key + last) / 2;
            rangeMap.put(Range.closedOpen(mid,newMid),last);
            last = key;
            mid = newMid;
            if (i == listD.size() - 1) {
                rangeMap.put(Range.closedOpen(newMid,key),key);
            }
        }
        Map<String,Double> geMap = new HashMap<>();
        Map<Double,List<String>> reverseMap = new HashMap<>();
        List<Double> keys = new ArrayList<>();
        for(int i = 10; i <= 99 ; i++) {
            for(int j = 10; j <= 99 ; j++) {
                int value = (int)Math.round(i*1000.0/j);
                System.out.println(i+ "/" + j + " " + value/10.0 + "%");
                double key = value/10.0;
                reverseMap.putIfAbsent(key, new ArrayList<>());
                reverseMap.get(key).add(i+ "/" + j);
                if (!keys.contains(key)) {
                    keys.add(key);
                }
                geMap.put(i+ "/" + j, value/1000.0);
            }
        }
        keys.sort(Comparator.naturalOrder());
        for (double key : keys) {
            List<String> strs = reverseMap.get(key);
            if (strs == null && strs.size() == 0) {
                System.out.println("错误：" + key);
            }
            for (String str : strs) {
                System.out.println(key + "% " + str);
            }
        }
        while (true) {
            System.out.println("********************************************");
            int v1 = (int)(100+Math.random()*(999-100+1));
            int v2 = (int)(100+Math.random()*(999-100+1));
            System.out.println(v1);
            System.out.println("---");
            System.out.println(v2);

            Thread.sleep(10000);
            double v1_s = v1;
            double v2_s = v2;

            double realVal = v1*1.0/v2;

            double su = 1.0;
            while (realVal > 10) {
                realVal *= 0.1;
                su *= 10.0;
            }
            while (realVal < 1) {
                realVal *= 10.0;
                su *= 0.1;
            }

            if (su < 1.0) {
                v1_s /= su;
            } else if (su > 10.0) {
                v2_s *= su;
            }

            double use = rangeMap.get(realVal);

            double firstResult = v2_s * use;
            System.out.println("STEP1: 计算" + v2_s + " * " + use + " = " + firstResult);
            double thirdResult = v1_s - firstResult;
            String pre = "";
            int suf = 1;
            if (thirdResult < 0) {
                thirdResult = -thirdResult;
                pre = "-";
                suf = -1;
            }
            System.out.println("STEP2: 计算" + v1_s + " - " + firstResult + " = " + pre + thirdResult);
            double result = 0.0;
            if (thirdResult != 0) {
                double shen = thirdResult/v2_s;

                double su_s = 1.0;
                while (shen > 10) {
                    shen *= 0.1;
                    su_s *= 10.0;
                }
                while (shen < 1) {
                    shen *= 10.0;
                    su_s *= 0.1;
                }

                int shen_i = (int)Math.round(shen);
                shen = shen_i * su_s;
                System.out.println("STEP3: 计算" + thirdResult + "/" + v2_s + " = " + shen);
                result = use + suf*shen;
                System.out.println("STEP4: 计算" + use + " + " + pre + shen + " = " + result);
            } else {
                result = use;
            }

            int estimateVal = (int)Math.floor(result*su*1000.0);
            System.out.println("estimate: " + estimateVal/10.0 + "%");
            int intVal = (int)Math.floor(v1*1000.0/v2);
            System.out.println("real: " + intVal/10.0 + "%");
            System.out.println("********************************************");
            Thread.sleep(3000);
        }


    }

}
